import { getToken, setToken } from '@/utils/auth.js'
import { getUserInfo, setUserInfo } from '@/utils/storageUserInfo.js'
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    tokenInfo: getToken() || {}, // token
    userInfo: getUserInfo() || {}
  },
  mutations: {
    mSetTokenInfo (state, initTokenInfo) { // 存token
      try {
        state.tokenInfo = initTokenInfo
        setToken(initTokenInfo) // 将token存到local上
      } catch (err) {
        console.log(err)
        return false
      }
    },
    mUserInfo (state, initUserInfo) { // 存用户个人信息
      state.userInfo = initUserInfo
      setUserInfo(initUserInfo)
    },
    // 用来更新用户名
    mUpdateName (state, newName) {
      state.userInfo.name = newName
      setUserInfo(state.userInfo)
    },
    mUpdateGender (state, newGender) {
      state.userInfo.gender = newGender
      setUserInfo(state.userInfo)
    },
    mUpdateBirthday (state, birthday) {
      state.userInfo.birthday = birthday
      setUserInfo(state.userInfo)
    },
    mUpdatePhoto (state, photo) {
      state.userInfo.photo = photo
      setUserInfo(state.userInfo)
    }
  },
  getters: {
    isLogin (state) { // 是否登录状态
      return Boolean(state.tokenInfo.token)
    },
    userPhoto (state) {
      return state.userInfo.photo
    }
  },
  actions: {
  },
  modules: {
  }
})
